package com.example.safelib.fragment


import androidx.fragment.app.Fragment
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.lifecycle.Observer
import com.example.baselib.fragment.BaseFragment
import com.example.baselib.utils.TimeUtils

import com.example.safelib.R
import com.example.safelib.SafeViewModel
import com.example.safelib.bean.SafeSenSorMoreBean
import kotlinx.android.synthetic.main.fragment_safe_sensor_info.*
import java.lang.Exception

/**
 * A simple [Fragment] subclass.
 */
class SafeSensorInfoFragment : BaseFragment<SafeViewModel>() {
    lateinit var sensornum: String
    override fun providerVMClass(): Class<SafeViewModel>? {
        return SafeViewModel::class.java
    }

    override fun contentLayout(): Int {
        return R.layout.fragment_safe_sensor_info
    }

    override fun initView() {
    }

    override fun initData() {
        val bundle = arguments!!
        sensornum = bundle.getString("sensornum")!!
        viewModel.getSenMoreInfo(sensornum).observe(this, Observer {
            initSersorsData(it)
            safe_sen_info_empty.visibility=View.INVISIBLE
        })
        if (!viewModel.getUnitInfo().isNullOrEmpty()){
            safe_sen_info_nickname.text=viewModel.getUnitInfo()["unitname"]
        }
    }

    private fun initSersorsData(data: SafeSenSorMoreBean) {
        info_st_code.text = data.stationnum
        info_sen_code.text = data.sensornum
//        info_type_code.text = data.sensortype.toString()
        info_type_name.text = data.typename
//        info_name_code.text = data.sensorcode
        info_sen_name.text = data.sensorname
        info_sen_local.text = data.sensorplace
        info_sen_unit.text = data.sensorunit
        info_update_time.text = TimeUtils.timeFormat(data.createtime)
        info_arrange_up.text = data.arrangeup.toString()
        info_arrange_down.text = data.arrangedown.toString()
        info_warn_up.text = data.warningvalueup
        info_warn_down.text = data.warningvaluedown
        info_lwarn_up.text = data.lwarningvalueup
        info_lwarn_down.text = data.lwarningvaluedown
        info_poweroff_up.text = data.poweroffvalueup
        info_poweroff_down.text = data.poweroffvaluedown
        info_poweron_up.text = data.poweronvalueup
        info_poweron_down.text = data.poweronvaluedown
    }

    override fun requestError(it: Exception?) {
        safe_sen_info_empty.visibility=View.VISIBLE
        if (it?.message=="暂无数据"){
            safe_sen_info_empty.findViewById<TextView>(R.id.err_txt).text=it?.message
            safe_sen_info_empty.findViewById<TextView>(R.id.err_txt).textSize=18f
            safe_sen_info_empty.findViewById<TextView>(R.id.err_msg).visibility=View.INVISIBLE
//            safe_sen_info_empty.findViewById<ImageView>(R.id.err_img).visibility=View.INVISIBLE
        }else{
            super.requestError(it)
            safe_sen_info_empty.findViewById<TextView>(R.id.err_msg).text = it?.message
        }

        safe_sen_info_empty.findViewById<Button>(R.id.err_rest_btn).setOnClickListener {  viewModel.loadSensorInfo(sensornum) }
    }
}
